"""
数据库连接查询
"""
from pymysql import Connection

conn = None
cursor = None

try:
    #1.创建数据库连接对象
    conn = Connection(
        user="root",
        password="123456",
        database="mydb1",
        host="localhost",
        charset="utf8",
        port=3306
    )
    #2.创建游标对象
    cursor = conn.cursor()
    #3.通过游标对象执行sql
    sql = '''
        SELECT * from hero
    '''
    res = cursor.execute(sql)
    #4.将数据提交到数据库或者获取数据
    query = cursor.fetchall()
    for item in query:
        print(item)
except Exception as e:
    print(e)
finally:
    #5.关闭游标对象
    cursor.close()
    #6.关闭数据库连接对象
    conn.close()
